package com.lzp.customview.bezier

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 * @description:
 * @author: lxp
 * @date: 2022/4/20 下午4:47
 */
class BezierTestView : View {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG).apply {
        color = Color.RED
        style = Paint.Style.STROKE
        strokeWidth = 20f
    }

    private val linePaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG).apply {
        color = Color.RED
        style = Paint.Style.STROKE
        strokeWidth = 2f
    }

    private val dr = 200
    private val path = Path()

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        path.moveTo(-dr.toFloat(), 0f)
        path.quadTo(-dr.toFloat(), dr.toFloat(), 0f, dr.toFloat())
        path.quadTo(dr.toFloat(), dr.toFloat(), dr.toFloat(), 0f)
        path.quadTo(dr.toFloat(), -dr.toFloat(), 0f, -dr.toFloat())
        path.quadTo(-dr.toFloat(), -dr.toFloat(), -dr.toFloat(), 0f)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //移动到中间，方便计算
        canvas.translate(width / 2f, height / 2f)
        canvas.drawLine(-width / 2f, 0f, width / 2f, 0f, linePaint)
        canvas.drawLine(0f, -height / 2f, 0f, height / 2f, linePaint)
        canvas.drawRect(Rect(-dr, -dr, dr, dr), linePaint)

        canvas.drawPath(path, paint)

    }

}